﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Security.Cryptography;

namespace d8
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private RNGCryptoServiceProvider rngSP = new RNGCryptoServiceProvider();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void cmdOK_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            RollDice();
        }

        private void lblD8_MouseDown(object sender, MouseButtonEventArgs e)
        {
            RollDice();
        }

        //-------------------------------------------

        private void RollDice()
        {
            int d8=-1; // result
            byte[] dwSeed = new byte[4];
            rngSP.GetBytes(dwSeed);
            int iSeed = BitConverter.ToInt32(dwSeed, 0);
            Random rnd = new Random(iSeed);

            d8 = rnd.Next(1, 9);

            lblD8.Content = d8.ToString();
            return;

            //---
            // проверка на равновероятное выпадение любой из сторон "кубика"

            int[] iar = new int[8];

            for (int i = 0; i < 8000; i++)
            {
                d8 = rnd.Next(1, 9);
                iar[d8 - 1]++;
            }

            d8 = d8;    // set breakpoint here and check the iar[] array
        }

        //-------------------------------------------

        private void RollDiceRNG()
        {
            int d8 = -1; // result
            byte[] dw = new byte[4];
            uint ui;

            do
            {
                rngSP.GetBytes(dw);
                ui = BitConverter.ToUInt32(dw, 0);
            }
            while (ui == uint.MaxValue);    // exclude maximum value.  ui == [0...uint.MaxValue)

            d8 = (int)((double)ui * (double)8 / (double)uint.MaxValue) + 1;  // ui/uint.MaxValue == [0...1)       d8 == [1...8]

            lblD8.Content = d8.ToString();
            return;

            //---
            // проверка на равновероятное выпадение любой из сторон "кубика"

            int[] iar = new int[8];

            for (int i = 0; i < 8000; i++)
            {
                do
                {
                    rngSP.GetBytes(dw);
                    ui = BitConverter.ToUInt32(dw, 0);
                }
                while (ui == uint.MaxValue);    // exclude maximum value.  ui == [0...uint.MaxValue)

                d8 = (int)((double)ui * (double)8 / (double)uint.MaxValue) + 1;  // ui/uint.MaxValue == [0...1)       d8 == [1...8]
                iar[d8 - 1]++;
            }

            d8 = d8;    // set breakpoint here and check the iar[] array
        }

    }
}
